<template>
	<view>
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText">返回</block>
		</cu-custom>
		<view class="register">

			<view class="content">
				<!-- 主体 -->
				<view>
					<uni-forms :value="formData" ref="form" labelWidth="130" labelPosition="top">
						<uni-forms-item required label="客户组织" name="organization">
							<uni-easyinput type="text" disabled="" v-model="formData.organizationName" autocomplete="off" />
						</uni-forms-item>
						<uni-forms-item required label="客户大类别" name="customerType">
							<uni-easyinput type="text" disabled="" v-model="formData.customerTypeName" autocomplete="off" />
						</uni-forms-item>
						<uni-forms-item required label="客户小类别" name="customerChildType">
							<view class="uni-list-cell-right">
								<picker :range="customerTypeChildLists" :range-key="'fname'" @change="bindCustomerTypeChildPickerChange">
									<view class="uni-input">{{formData.customerTypeChildName}}</view>
								</picker>
							</view>
						</uni-forms-item>
						<uni-forms-item required label="业务员" name="salesman">
							<view class="uni-list-cell-right">
								<picker :range="sales" :range-key="'fname'" @change="bindSalesmanPickerChange">
									<view class="uni-input">{{formData.bizName}}</view>
								</picker>
							</view>
						</uni-forms-item>
						<uni-forms-item required label="默认价格策略" v-show="false" name="priceStrategy" >
							<view class="uni-list-cell-right" style="background-color:#eee;">
								<picker :range="defaultPriceStrategyLists" :range-key="'strategyName'" @change="bindPriceStrategyPickerChange" disabled="">
									<view class="uni-input">{{selectStrategyName}}</view>
								</picker>
							</view>
						</uni-forms-item>
						<uni-forms-item required label="客户名称" name="name">
							<uni-easyinput type="text" v-model="formData.name" placeholder="请输入客户名称" autocomplete="off" />
						</uni-forms-item>
						<uni-forms-item required name="phoneNumber" label="手机号码">
							<uni-easyinput type="text" v-model="formData.phoneNumber" placeholder="请输入手机号码" autocomplete="off" />
						</uni-forms-item>
						<uni-forms-item required name="principal" label="负责人姓名">
							<uni-easyinput type="text" v-model="formData.principal" placeholder="请输入负责人姓名" autocomplete="off" />
						</uni-forms-item>
						<uni-forms-item required label="送货地址" name="address">
							<view class="uni-list-cell-right">
								<input @click="WebaddressChange" type="text" autocomplete="off" placeholder="省份、城市、区县" v-model="this.deliveryAddress"
								 disabled="false" focus="false" />
								<w-picker mode="region" :defaultVal="defaultVal" @confirm="onConfirm" ref="region" themeColor="#f00"></w-picker>
							</view>
						</uni-forms-item>
						<uni-forms-item name="address" label="详细地址">
							<uni-easyinput type="text" v-model="formData.address" autocomplete="off" placeholder="请输入详细地址" />
						</uni-forms-item>
						<uni-forms-item required name="password" label="密码">
							<uni-easyinput type="password" v-model="formData.password" autocomplete="off" placeholder="请输入密码" />
						</uni-forms-item>
						<uni-forms-item required name="passwordConfirm" label="确认密码">
							<uni-easyinput type="password" v-model="formData.passwordConfirm" autocomplete="off" placeholder="请输入确认密码" />
						</uni-forms-item>

						<!-- <uni-forms-item class="policy">
							<label>注册/登录即表示您同意</label><a href="#">《********客户服务协议》</a>
						</uni-forms-item> -->
					</uni-forms>
					<wButton text="注册" :rotate="isRotate" @click.native="startReg()"> </wButton>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getOrganization,
		getCustomerTypeList,
		getCustomerTypeChildrenList,
		getDefalutPriceStrategyList,
		getBizList,
		registCustomer
	} from "@/server/config.js";
	import {
		quit
	} from "@/server/api.js";
	import wInput from "@/components/watch-login/watch-input.vue"; //input
	import wButton from "@/components/watch-login/watch-button.vue"; //button

	export default {
		onShow() {

		},
		onLoad(options) {
			this.fetchData()
		},
		data() {
			return {
				// logoImage: 'https://imgt1.oss-cn-shanghai.aliyuncs.com/ecAllRes/images/logo.png',
				logoImage: "",
				reg: /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/,
				formData: {
					name: '',
					organizationCode: '',
					organizationName: '',
					customerTypeCode: '',
					customerTypeName: '',
					customerTypeChildCode: '',
					customerTypeChildName: '',
					bizCode: '',
					bizName: '',
					principal: '',
					priceStrategy: '',
					phoneNumber: '',
					province: '',
					city: '',
					district: '',
					address: '',
					password: '',
					passwordConfirm: '',
				},
				deliveryAddress: '',
				organizationes: [],
				customerTypes: [],
				customerTypeChildLists: [],
				sales: [],
				defaultPriceStrategyLists:[],
				selectStrategyName:'',
				mailData: '', // 邮箱
				passData: '', //密码
				verCode: "", //验证码
				showAgree: true, //协议是否选择
				isRotate: false, //是否加载旋转
				tagtype: '',
				user_id: '',
				recode: '',
				parent_id: '',
				defaultVal: [10, 0, 5],
			}
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {

		},
		methods: {

			async fetchData() {

				//组织
				let res = await getOrganization()
				this.organizationes = res.data

				//大分类
				res = await getCustomerTypeList()
				this.customerTypes = res.data
				if (res.data.length > 0) {
					res = await getCustomerTypeChildrenList(res.data[0].fid)
					this.customerTypeChildLists = res.data
					if (this.customerTypeChildLists.length > 0) {
						this.formData.customerTypeChildCode = this.customerTypeChildLists[0].fnumber
						this.formData.customerTypeChildName = this.customerTypeChildLists[0].fname
					}
				}
				
				//默认价格策略
				res = await getDefalutPriceStrategyList()
				this.defaultPriceStrategyLists = res.data
				
				if(res.data.length> 0){
					this.formData.priceStrategy = res.data[0].id
					this.selectStrategyName =res.data[0].strategyName 
				}
				
				this.formData.organizationCode = this.organizationes[2].fnumber
				this.formData.organizationName = this.organizationes[2].fname
				
				//客户大分类
				this.formData.customerTypeCode = this.customerTypes[0].fid
				this.formData.customerTypeName = this.customerTypes[0].fname
				
				
				res = await getBizList(this.formData.customerTypeChildName,this.formData.organizationCode)
				this.sales = res.data
				if(this.sales.length > 0){
					this.formData.bizCode = this.sales[0].fnumber
					this.formData.bizName = this.sales[0].fname
				}
				
			},

			bindOrganizationPickerChange(e) {
				this.formData.organizationCode = this.organizationes[e.target.value].fnumber
				this.formData.organizationName = this.organizationes[e.target.value].fname
			},
			bindCustomerTypePickerChange(e) {
				let customerTypeIndex = e.target.value
				this.formData.customerTypeCode = this.customerTypes[customerTypeIndex].fid
				this.formData.customerTypeName = this.customerTypes[customerTypeIndex].fname

				let _this = this;
				//根据fid获取到小分类
				getCustomerTypeChildrenList(this.formData.customerTypeCode).then(res => {
					this.customerTypeChildLists = res.data
					if (res.data.length > 0) {
						_this.formData.customerTypeChildName = res.data[0].fname
						_this.formData.customerTypeChildCode = res.data[0].fid
					} else {
						_this.formData.customerTypeChildName = ''
						_this.formData.customerTypeChildCode = ''
					}
				})
			},
			bindCustomerTypeChildPickerChange(e) {
				this.formData.customerTypeChildCode = this.customerTypeChildLists[e.target.value].fnumber
				this.formData.customerTypeChildName = this.customerTypeChildLists[e.target.value].fname
				
				let _this = this;
				//查询业务员
				getBizList(this.formData.customerTypeChildName,this.formData.organizationCode).then(res=>{
					this.sales = res.data
					if (res.data.length > 0) {
						_this.formData.bizName = res.data[0].fname
						_this.formData.bizCode = res.data[0].fnumber
					} else {
						_this.formData.bizName = ''
						_this.formData.bizCode = ''
					}
				})
			},
			bindSalesmanPickerChange(e) {
				let salesmanIndex = e.target.value
				this.formData.bizCode = this.sales[salesmanIndex].fnumber
				this.formData.bizName = this.sales[salesmanIndex].fname
			},
			bindPriceStrategyPickerChange(e){
				let index = e.target.value
				this.formData.priceStrategy = this.defaultPriceStrategyLists[index].id
				this.selectStrategyName = this.defaultPriceStrategyLists[index].strategyName
			},
			onConfirm(val) {
				this.deliveryAddress = val.checkArr[0] + " " + val.checkArr[1] + " " + val.checkArr[2];
				this.address_id_list = val.areaVal;
				this.formData.province = val.areaVal[0]
				this.formData.city = val.areaVal[1]
				this.formData.district = val.areaVal[2]
			},

			async logo() {
				this.logoImage = '/yoush/static/images/ysh.jpeg';
			},

			async WebaddressChange() {
				this.$refs["region"].show();
			},
			async startReg() {

				if (this.formData.customerTypeChildName == '') {
					uni.showToast({
						title: "客户小类别不能为空", //提示的内容,
						duration: 1000, //延迟时间,
						icon: "none",
						mask: true, //显示透明蒙层，防止触摸穿透,
						success: res => {}
					});
					return
				}

				if (this.formData.name == '') {
					uni.showToast({
						title: "客户名称不能为空", //提示的内容,
						duration: 1000, //延迟时间,
						icon: "none",
						mask: true, //显示透明蒙层，防止触摸穿透,
						success: res => {}
					});
					return
				}

				if (this.formData.phoneNumber == '') {
					uni.showToast({
						title: "手机号码不能为空", //提示的内容,
						duration: 1000, //延迟时间,
						icon: "none",
						mask: true, //显示透明蒙层，防止触摸穿透,
						success: res => {}
					});
					return
				}

				if (this.formData.principal == '') {
					uni.showToast({
						title: "负责人姓名不能为空", //提示的内容,
						duration: 1000, //延迟时间,
						icon: "none",
						mask: true, //显示透明蒙层，防止触摸穿透,
						success: res => {}
					});
					return
				}

				if (this.deliveryAddress == '') {
					uni.showToast({
						title: "送货地址不能为空", //提示的内容,
						duration: 1000, //延迟时间,
						icon: "none",
						mask: true, //显示透明蒙层，防止触摸穿透,
						success: res => {}
					});
					return
				}

				if (this.formData.password == '') {
					uni.showToast({
						title: "密码不能为空", //提示的内容,
						duration: 1000, //延迟时间,
						icon: "none",
						mask: true, //显示透明蒙层，防止触摸穿透,
						success: res => {}
					});
					return
				}

				if (this.formData.passwordConfirm == '') {
					uni.showToast({
						title: "确认密码不能为空", //提示的内容,
						duration: 1000, //延迟时间,
						icon: "none",
						mask: true, //显示透明蒙层，防止触摸穿透,
						success: res => {}
					});
					return
				}

				if (this.formData.password != this.formData.passwordConfirm) {
					uni.showToast({
						title: "两次密码输入不一致", //提示的内容,
						duration: 1000, //延迟时间,
						icon: "none",
						mask: true, //显示透明蒙层，防止触摸穿透,
						success: res => {}
					});
					return
				}

				//提交数据
				let res = await registCustomer(this.formData)
				if (res.statusCode == 200) {
					uni.showToast({
						title: "注册信息已提交，等待审批", //提示的内容,
						duration: 5000, //延迟时间,
						icon: "none",
						mask: true, //显示透明蒙层，防止触摸穿透,
						success: res => {}
					});

					setTimeout(function() {
						uni.navigateTo({
							url: "/apiPam/logincommon/main"
						});
					}, 1000)
				}

				if (res.statusCode == 400) {
					let errors = res.data.errors
					errors.forEach((p) => {
						let message = p.message
						uni.showToast({
							title: message, //提示的内容,
							duration: 2000, //延迟时间,
							icon: "none",
							mask: true, //显示透明蒙层，防止触摸穿透,
							success: res => {}
						});
						return;
					})
				}


			},
		}
	};
</script>

<style lang='scss' scoped>
	@import url("../../components/watch-login/css/icon.css");
	@import "./style";
</style>
